package design.pattern.Lu18MementoPattern;

/**
 * @program JavaBase
 * @description: 备忘录模式
 *  违反迪米特法则
 * @author: zhanglu
 * @create: 2020-04-10 15:39:00
 */
public class MementoPattern01 {

    public static void main(String[] args) {
        Boy b1 = new Boy();
        b1.setState("感觉不错");

        Memento memento = b1.createMemento();

        b1.print();
        b1.changeState();
        b1.print();

        b1.restoreMemento(memento);
        b1.print();

    }

}

class Boy{

    private String state;

    public void changeState(){
        this.state = "不太好";
    }

    public void setState(String state){
        this.state = state;
    }

    public String getState(){
        return this.state;
    }

    public void print(){
        System.out.println(this.state);
    }

    public Memento createMemento(){
        return new Memento(this.state);
    }

    public void restoreMemento(Memento memento){
        this.state = memento.getState();
    }

}

class Memento{

    private String state;

    public Memento(String state){
        this.state = state;
    }
    public String getState(){
        return this.state;
    }

}
